		//
//  VisitorViewController.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/4.
//  Copyright © 2015年 002. All rights reserved.
//

#import "VisitorViewController.h"
#import "OAuthViewController.h"
#import "UserAccountViewModel.h"

@interface VisitorViewController ()<VisitorViewDelegate>

/**
 *  用户是否登录
 */
@property(nonatomic,assign) BOOL isLogion;

@end

@implementation VisitorViewController

#pragma mark - 视图生命周期
- (void)loadView {
    
    // 如果未登录，把当前控制器的根视图替换为 ‘访客视图’
    self.isLogion ? [super loadView] : [self setupVisitorView];
    NSLog(@"self.isLogion:%d",self.isLogion);
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    NSLog(@"%@",self.visitorLoginView);
}

/**
 *  设置访客视图
 */
- (void)setupVisitorView {

    self.visitorLoginView = [[VisitorView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.visitorLoginView;

    // 设置代理
    self.visitorLoginView.delegate = self;
    self.visitorLoginView.registerBlock = ^{
        NSLog(@"用户注册");
    };
    self.visitorLoginView.loginBlock = ^{
        __weak typeof(self) weakSelf = self;
        [weakSelf visitorViewDidLogin];
    };
    
    // 添加导航栏按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(visitorLoginViewDidRegister)];       // 注册按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(visitorViewDidLogin)];  // 登录按钮
}

#pragma mark - 代理方法 VisitorLoginViewDelegate
- (void)visitorLoginViewDidRegister {
    NSLog(@"用户注册");
}

- (void)visitorLoginViewDidLogin {
    NSLog(@"用户登录");
}

#pragma mark - 访客视图监听方法
- (void)visitorViewDidLogin {
    
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:[OAuthViewController new]];
    [self presentViewController:nvc animated:YES completion:nil];
}

#pragma mark - 设置属性
/**
 *  用户是否登录
 *
 *  @return 
 */
- (BOOL)isLogion {
    
    return [UserAccountViewModel sharedAccountViewModel].userLogin;
}

@end
